//
//  UIButton+Extension.swift
//  CosClub
//
//  Created by 沙睿 on 2020/2/7.
//  Copyright © 2020 ios. All rights reserved.
//
import UIKit

extension UIButton {
    
    public convenience init(_ text: String? = "", color: UIColor = UIColor.app.c3a, fontSize: CGFloat = 12, type:FontType = FontType.nomal, cornerRadius: CGFloat = 0) {
        self.init()
        setAttributes(text,color: color, fontSize: fontSize, type: type, cornerRadius: cornerRadius)
        
    }
    
    public func setAttributes(_ text: String? = "", color: UIColor = UIColor.app.c3a, fontSize: CGFloat = 12, type:FontType = FontType.nomal, cornerRadius: CGFloat = 0) -> Void{
        self.setTitle(text, for: .normal)
        self.setTitleColor(color, for: .normal)
     
        
        switch type {
        case .nomal:
            self.titleLabel?.font = UIFont.sr_system(fontSize: fontSize)
        case .bold:
            self.titleLabel?.font = UIFont.sr_bold(fontSize: fontSize)
        case .number:
            self.titleLabel?.font = UIFont.sr_number(fontSize: fontSize)
        }
        if cornerRadius > 0 {
            self.layer.cornerRadius = cornerRadius
            self.layer.masksToBounds = true
        }
    }
    
    
    public convenience init(submit text:String?, fontSize: CGFloat = 16, cornerRadius: CGFloat = 0, weight: UIFont.Weight = .regular) {
        self.init()
        
        self.setTitle(text, for: .normal)
        
        self.titleLabel?.font = UIFont.systemFont(ofSize: fontSize, weight: weight)
        self.imageView?.contentMode = .scaleToFill
        
        UIImage.color("EBEBEB".color(), cornerRadius: cornerRadius, stretch: true){self.setBackgroundImage($0, for: .disabled)}
        UIImage.color(UIColor.app.c1, cornerRadius: cornerRadius, stretch: true){self.setBackgroundImage($0, for: .normal)}
        
        self.setTitleColor(UIColor.app.white, for: .disabled)
        self.setTitleColor(UIColor.black, for: .normal)
        
    }
    
    
    public enum RGButtonImagePosition {
        case top          //图片在上，文字在下，垂直居中对齐
        case bottom       //图片在下，文字在上，垂直居中对齐
        case left         //图片在左，文字在右，水平居中对齐
        case right        //图片在右，文字在左，水平居中对齐
    }
    /// - Description 设置Button图片的位置
    /// - Parameters:
    ///   - style: 图片位置
    ///   - spacing: 按钮图片与文字之间的间隔
    public func imagePosition(style: RGButtonImagePosition, spacing: CGFloat) {
        //得到imageView和titleLabel的宽高
        
        self.layoutIfNeeded()
        let imageWidth = self.imageView?.frame.size.width
        let imageHeight = self.imageView?.frame.size.height
        
        var labelWidth: CGFloat! = 0.0
        var labelHeight: CGFloat! = 0.0
        
        labelWidth = self.titleLabel?.intrinsicContentSize.width
        labelHeight = self.titleLabel?.intrinsicContentSize.height
        
        //初始化imageEdgeInsets和labelEdgeInsets
        var imageEdgeInsets = UIEdgeInsets.zero
        var labelEdgeInsets = UIEdgeInsets.zero
        
        //根据style和space得到imageEdgeInsets和labelEdgeInsets的值
        switch style {
        case .top:
            //上 左 下 右
            imageEdgeInsets = UIEdgeInsets(top: -labelHeight-spacing/2, left: 0, bottom: 0, right: -labelWidth)
            labelEdgeInsets = UIEdgeInsets(top: 0, left: -imageWidth!, bottom: -imageHeight!-spacing/2, right: 0)
            break;
            
        case .left:
            imageEdgeInsets = UIEdgeInsets(top: 0, left: -spacing/2, bottom: 0, right: spacing)
            labelEdgeInsets = UIEdgeInsets(top: 0, left: spacing/2, bottom: 0, right: -spacing/2)
            break;
            
        case .bottom:
            imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: -labelHeight!-spacing/2, right: -labelWidth)
            labelEdgeInsets = UIEdgeInsets(top: -imageHeight!-spacing/2, left: -imageWidth!, bottom: 0, right: 0)
            break;
            
        case .right:
            imageEdgeInsets = UIEdgeInsets(top: 0, left: labelWidth+spacing/2, bottom: 0, right: -labelWidth-spacing/2)
            labelEdgeInsets = UIEdgeInsets(top: 0, left: -imageWidth!-spacing/2, bottom: 0, right: imageWidth!+spacing/2)
            break;
            
        }
        
        self.titleEdgeInsets = labelEdgeInsets
        self.imageEdgeInsets = imageEdgeInsets
        
    }
    
    public func diffrentTitle(string: String, foneSize: CGFloat,color: UIColor, state: UIControl.State) -> Void{
        let att = NSAttributedString(string: string,
                                     attributes: [NSAttributedString.Key.font: UIFont.sr_system(fontSize: foneSize),
                                                  NSAttributedString.Key.foregroundColor: color])
        self.setAttributedTitle(att, for: state)
        
    }
}
